
package com.yuan.view;

import com.yuan.graduate.R;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.unit.PreferenceUtil;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 带有CheckBox的一个横条
 * 
 * @author YUAN
 */
public class CheckBoxPreference extends LinearLayout implements PreferenceKeys, OnClickListener
{

    private TextView mSummary = null;
    private ImageView imageView = null;
    private Context mContext = null;
    private String key = null;
    private LinearLayout layout = null;

    public CheckBoxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CheckBoxPreference(Context context) {
        this(context, null);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.settings_checkbox_preference, this);
        mContext = context;
        mSummary = (TextView) findViewById(R.id.summary);
        imageView = (ImageView) findViewById(R.id.imageView);
        layout = (LinearLayout) findViewById(R.id.setting_item);
        layout.setOnClickListener(this);
    }

    public void setTitle(String title) {
        if (TextUtils.isEmpty(title) == false) {
            mSummary.setText(title);
        }
    }

    public void setSummary(String summary) {
        if (TextUtils.isEmpty(summary) == false) {
            mSummary.setVisibility(View.VISIBLE);
            mSummary.setText(summary);
        }
    }

    public void setSummary(int summary) {
        mSummary.setVisibility(View.VISIBLE);
        mSummary.setText(mContext.getResources().getString(summary));
    }

    /***
     * 设置checkbox的初始状态
     * 
     * @param b
     */
    public void setOriginalChecked(boolean b) {
        imageView.setSelected(b);
    }

    public String getKey() {
        return key;
    }

    /**
     * 设置ＫＥＹ值　并对按钮点击状态进行初始化
     * 
     * @param key
     */
    public void setKey(String key) {
        this.key = key;
        if (key != null)
        {
            boolean flag = PreferenceUtil.getInstance().getBoolean(key, false);
            imageView.setSelected(flag);
        }
    }

    public void hideLine() {
        findViewById(R.id.line).setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        boolean flag = PreferenceUtil.getInstance().getBoolean(key, false);
        flag = !flag;
        imageView.setSelected(flag);
        PreferenceUtil.getInstance().putBoolean(key, flag);
    }

    public void onDestroy() {
        // TODO Auto-generated method stub
        mSummary = null;
        imageView = null;
        mContext = null;
        key = null;
        layout = null;

    }

}
